//
//  SortOtherCollectionViewCell.m
//  MountEverestFM
//
//  Created by dllo on 15/10/31.
//  Copyright © 2015年 chenn. All rights reserved.
//

#import "SortOtherCollectionViewCell.h"

#import "SortDetailedTVC.h"

@implementation SortOtherCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.tableView.backgroundColor=[UIColor whiteColor];
        self.tableView=[[UITableView alloc]init];
        self.tableView.delegate=self;
        self.tableView.dataSource=self;
        [self.contentView addSubview:self.tableView];
    }
    return self;
        
}
-(void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    self.tableView.frame=self.contentView.bounds;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SortDetailedTVC *cell=[tableView dequeueReusableCellWithIdentifier:@"othercell"];
    if (!cell) {
        cell =[[SortDetailedTVC alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"othercell"];
           }

    cell.lable1.text=[[self.arr objectAtIndex:indexPath.row] objectForKey:@"title"];
    cell.lable2.text=[[self.arr objectAtIndex:indexPath.row] objectForKey:@"intro"];
    CGFloat f=[[[self.arr objectAtIndex:indexPath.row] objectForKey:@"playsCounts"] floatValue];
    if (f<10000) {
        cell.lable3.text=[NSString stringWithFormat:@"%.0f",f];
    }else{
        cell.lable3.text=[NSString stringWithFormat:@"%.2f万",f/10000];
    }

    
    
//    cell.lable3.text=[[[[self.dic objectForKey:@"list"] objectAtIndex:indexPath.row] objectForKey:@"playsCounts"] stringValue];
    cell.lable4.text=[[[self.arr objectAtIndex:indexPath.row] objectForKey:@"tracksCounts"] stringValue];
    [cell.imageV1 sd_setImageWithURL:[[self.arr objectAtIndex:indexPath.row] objectForKey:@"albumCoverUrl290"] placeholderImage:[UIImage imageNamed:@"find_kind_btn_default@3x.png"]];
    cell.imageV2.image=[UIImage imageNamed:@"sound_playtimes@3x"];
    cell.imageV3.image=[UIImage imageNamed:@"album_tracks@3x.png"];
    
    cell.imV.image=[UIImage imageNamed:@"cell_arrow_all.png"];


    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return FM_Screen_Pts_Width*70+FM_Screen_Pts_Height*40;
}



- (void)setStr:(NSString *)str
{
    if (_str!=str) {
        _str=str;
        
    }
    self.page = 1;
    [self setupRefresh];
}

-(void)netWorking
{
    if (!self.isRefresh) {
        self.page++;
    }else{
        self.page = 1;
    }
    
    [NetWorkManager GET:[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/category/album?calcDimension=hot&categoryId=%@&device=iPhone&pageId=%ld&pageSize=20&status=0&tagName=%@",self.num,self.page,self.str ] isCaChe:YES resultData:^(id responseObject) {
//        self.dic=responseObject;
        if (responseObject != nil) {
            if (self.page == 1) {
                self.arr = [NSMutableArray array];
            }
            self.arr = [self.arr arrayByAddingObjectsFromArray:[responseObject objectForKey:@"list"]];
            [self.tableView reloadData];
            [self setupEndRefresh];
        }
        // 没有更多了,移除加载更多
        if ([[responseObject objectForKey:@"maxPageId"] integerValue] == self.page) {
            self.tableView.footer = nil;
        }else{
            // 尾
            self.tableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
                self.isRefresh = NO;
                [self netWorking];
            }];
        }
    } resultRrror:^(NSError *error) {
        
    }];
}

// 设置刷新
- (void)setupRefresh
{
    // 头
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.isRefresh = YES;
        [self netWorking];
    }];
    [self.tableView.header beginRefreshing];
    
    // 尾
    self.tableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.isRefresh = NO;
        [self netWorking];
    }];
}

// 结束刷新
- (void)setupEndRefresh
{
     self.tableView.frame=self.contentView.bounds;
    [self.tableView.header endRefreshing];
    [self.tableView.footer endRefreshing];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    self.block([self.arr objectAtIndex:indexPath.row]);
}





@end
